package utwente.hmi.touchmol.elements;

import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.beans.PropertyChangeSupport;
import javax.swing.event.MouseInputListener;

/**
 * Helper class that stores a screen with its position. It also listens for mouse
 * events that are targeted on this screenshot. Referred to as bookmarks.
 * @author FikkertFW, adapted by Jeroen Logtenberg
 */
public class Screenshot
        implements MouseInputListener
{
   public static final String PROPERTY_RESTORE_SCREENSHOT = "restoreScreenshot";
   public static final double FACTOR_WIDTH = 0.2;
   public static final double FACTOR_HEIGHT = 0.2;
   
   public BufferedImage screen;
   public AffineTransform transform;
   public int x,y;
   public double rotation,width,height;
   public int id;
   private MouseEvent last;
   public ScreenshotPanel canvas;
   public String script;
   public final PropertyChangeSupport pcs = new PropertyChangeSupport(this);
   public boolean isActive = false;
   public int zoom = 100;
   
   public Screenshot(BufferedImage _screen, int _id, String _script)
   {
      screen    = _screen;
      transform = new AffineTransform();
      x         = 0;
      y         = 0;
      rotation  = 0;
      width     = FACTOR_WIDTH;
      height    = FACTOR_HEIGHT;
      id        = _id;
      script    = _script;
   }
   
   // empty functions
   public void mouseEntered(MouseEvent e){}
   public void mouseExited(MouseEvent e){}
   public void mouseMoved(MouseEvent e){}
   public void mousePressed(MouseEvent e){}

   /**
    * Remove screenshot on button3 mouse click event
    * @param e
    */
   public void mouseClicked(MouseEvent e)
   {
      if (e.getButton() == MouseEvent.BUTTON2 && clickOnMe(e))
      {
         if (canvas != null)
         {
            if (canvas.removeScreenshot(this))
               System.out.println("removed me");
            else
               System.out.println("could not remove me");
            canvas.repaint();
         }
      }
      else if (e.getButton() == MouseEvent.BUTTON3 && clickOnMe(e))
      {
         pcs.firePropertyChange(PROPERTY_RESTORE_SCREENSHOT, null, this);
      }
   }

   /**
    * Resets mouse pointer position
    * @param e
    */
   public void mouseReleased(MouseEvent e)
   {
      last = null;
      canvas.setOnTop(null);
   }

   private boolean clickOnMe(MouseEvent e)
   {
      if (x < e.getX() && e.getX() < (x + screen.getWidth()*width)  &&
          y < e.getY() && e.getY() < (y + screen.getHeight()*height)   )
      {
         return true;
      }
      return false;
   }
   
   /**
    * Checks if there is a screenshot at a certain location.
    * @author LogtenbergJD
    * @param p The point that should be checked.
    * @return If there is a screenshot at the location of p
    */
   public boolean clickOnMe(Point p)
   {
      if (x < p.x && p.x < (x + screen.getWidth()*width)  &&
          y < p.y && p.y < (y + screen.getHeight()*height)   )
      {
         return true;
      }
      return false;
   }

   public void mouseDragged(MouseEvent e)
   {
      if (last != null && canvas.onTop(this))
      {
         if (clickOnMe(e))
         {
            canvas.setOnTop(this);
            x += e.getX()-last.getX();
            y += e.getY()-last.getY();
         }
      }
      last = e;
      if (canvas != null)
         canvas.repaint();
   }

}  